package com.bt.lms.dao.servise.impl;

import com.bt.lms.entity.Major;
import com.bt.lms.entity.page.Page;
import com.bt.lms.dao.entity.RlMajorStudyCenPo;
import com.bt.lms.dao.servise.MajorDao;

import java.sql.*;
import java.util.List;

/**
 * 专业服务dao实现
 *
 * @author 徐大永
 */
public class MajorDaoImpl implements MajorDao {

    @Override
    public int insert(String sql, Major major) {

        //要入库的属性
        String name = major.getName();
        String code = major.getCode();
        Integer status = major.getStatus();
        String details = major.getDetails();
        try {
            //获取连接
            Connection connection = UserDaoImpl.pond();
            //域加载对象 INSERT INTO major (name,code,status,details) VALUES (?,?,?,?)
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, name);
            preparedStatement.setString(2, code);
            preparedStatement.setInt(3, status);
            preparedStatement.setString(4, details);
            //执行SQL
            int i = preparedStatement.executeUpdate();
            //关闭资源
            preparedStatement.close();
            connection.close();

            return i;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int insertRls(String sql, List<Long> majorIds, Long stuCenId) {
        //要入库的属性
        try {
            //获取连接
            Connection connection = UserDaoImpl.pond();
            //域加载对象 INSERT rl_major_study_centre (major_id,study_centre_id) VALUES (?,?)
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            for (Long majorId:majorIds){
                preparedStatement.setLong(1, majorId);
                preparedStatement.setLong(2, stuCenId);
                preparedStatement.addBatch();
            }
            //执行SQL
            int[] ints = preparedStatement.executeBatch();
            //关闭资源
            preparedStatement.close();
            connection.close();
            return ints.length;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Major select(String sql, Long id) {

        //要返回的对象  SELECT * FROM major WHERE id = ?
        Major major = new Major();
        //带走id
        major.setId(id);

        try {
            //连接
            Connection connection = UserDaoImpl.pond();
            //域处理
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setLong(1, id);
            //4.执行SQL
            ResultSet resultSet = preparedStatement.executeQuery();
            boolean next = resultSet.next();
            if (next) {

                String name = resultSet.getString("name");
                String code = resultSet.getString("code");
                int status = resultSet.getInt("status");
                String details = resultSet.getString("details");

                //带走
                major.setName(name);
                major.setCode(code);
                major.setStatus(status);
                major.setDetails(details);

            }
            //5.关闭资源
            preparedStatement.close();
            connection.close();

            return major;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public int update(String sql, Major major) {

        //要插入的数据
        String name = major.getName();
        String details = major.getDetails();
        Long id = major.getId();

        //连接
        Connection connection = UserDaoImpl.pond();
        try {
            //域处理 UPDATE major SET name=?,details=? WHERE id=?
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, name);
            preparedStatement.setString(2, details);
            preparedStatement.setLong(3, id);
            //执行
            int i = preparedStatement.executeUpdate();
            //关闭资源
            preparedStatement.close();
            connection.close();

            return i;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    public int update(String sql, Long id) {

        try {
            //连接
            Connection connection = UserDaoImpl.pond();
            //域处理
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setLong(1, id);
            //执行
            int i = preparedStatement.executeUpdate();
            //关闭连接
            preparedStatement.close();
            connection.close();
            return i;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public int insert(String sql, RlMajorStudyCenPo rlMajorStudyCenPo) {

        //入库的属性
        Long majorId = rlMajorStudyCenPo.getMajorId();
        Long studyCentreId = rlMajorStudyCenPo.getStudyCentreId();
        try {
            //连接
            Connection connection = UserDaoImpl.pond();
            //域处理对象
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setLong(1, majorId);
            preparedStatement.setLong(2, studyCentreId);
            //执行SQL
            int i = preparedStatement.executeUpdate();
            //关闭资源
            preparedStatement.close();
            connection.close();
            return i;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void query(String majorSql, Page<Major> page, List<Major> majors, String majorSumSql) {

        //开始位置
        Integer size = page.getSize();//每页数量
        Integer current = page.getCurrent();//页码
        int front = size * (current - 1);

        try {
            //连接
            Connection connection = UserDaoImpl.pond();
            //域执行对象
            PreparedStatement preparedStatement = connection.prepareStatement(majorSql);
            preparedStatement.setInt(1, front);
            preparedStatement.setInt(2, size);

            //执行（专业总数）
            ResultSet resultSet = preparedStatement.executeQuery(majorSumSql);
            resultSet.next();
            long aLong = resultSet.getLong(1);
            page.setTotal(aLong);

            //执行 (专业具体内容) 编码，名称，状态
            ResultSet majorRS = preparedStatement.executeQuery();
            while (majorRS.next()) {

                Major major = new Major();

                long id = majorRS.getLong("id");
                String code = majorRS.getString("code");
                String name = majorRS.getString("name");
                int status = majorRS.getInt("status");

                //带走
                major.setId(id);
                major.setCode(code);
                major.setName(name);
                major.setStatus(status);

                majors.add(major);
            }

            //关闭资源
            preparedStatement.close();
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
